/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jni;

import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.ptr.PointerByReference;
import dto.ProcessAndTitle;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author phat10130059
 */
public class WindowHandles {

    private static final int MAX_TITLE_LENGTH = 1024;
    private static final User32 user32 = User32.INSTANCE;
    private static List<ProcessAndTitle> list;

    public static List<ProcessAndTitle> getAllTile() {
        
        list = new ArrayList<ProcessAndTitle>();
        
        User32DLL.EnumWindows(new WinUser.WNDENUMPROC() {
            int count = 0;

            @Override
            public boolean callback(WinDef.HWND hWnd, Pointer arg1) {

                char[] buffer = new char[MAX_TITLE_LENGTH * 2];

                User32DLL.GetWindowTextW(hWnd, buffer, MAX_TITLE_LENGTH);
                String titleName = Native.toString(buffer);

                WinDef.RECT rectangle = new WinDef.RECT();
                user32.GetWindowRect(hWnd, rectangle);

                // get rid of this if block if you want all windows regardless of whether
                // or not they have text
                if (titleName.isEmpty()) {
                    return true;
                }

                if (!User32.INSTANCE.IsWindowVisible(hWnd) && rectangle.left > -32000) {
                    return true;
                }


                char[] buffer2 = new char[MAX_TITLE_LENGTH * 2];
                PointerByReference pointer = new PointerByReference();
                User32DLL.GetWindowThreadProcessId(hWnd, pointer);
                Pointer process = Kernel32.OpenProcess(Kernel32.PROCESS_QUERY_INFORMATION | Kernel32.PROCESS_VM_READ, false, pointer.getValue());
                Psapi.GetModuleBaseNameW(process, arg1, buffer2, MAX_TITLE_LENGTH);


                String processName = Native.toString(buffer2);

                if (!processName.equals("") && !processName.equals("Explorer.EXE")) {
                    //                    System.out.println("Found window with text " + hWnd + ", total " + ++count
                    //                            + " Text: " + titleName);
                    //                    System.out.println(processName);
                    ProcessAndTitle processAndTitle = new ProcessAndTitle(titleName.trim(), processName);
                    
                    list.add(processAndTitle);

                }


                return true;
            }
        }, Pointer.NULL);

        return list;
    }

//    public static void main(String[] args) {
//        List<ProcessAndTitle> allTile = getAllTile();
//
//        for (ProcessAndTitle processAndTitle : allTile) {
//            System.out.println(processAndTitle);
//        }
//    }
}
